package com.XDApp.xdbase.view;

import java.io.ByteArrayOutputStream;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.PaintDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.XDApp.xdbase.R;
import com.XDApp.xdbase.utils.Logger;
import com.XDApp.xdbase.utils.myutils.PromptManager;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeConfig;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.UMWXHandler;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.controller.listener.SocializeListeners.SocializeClientListener;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.WeiXinShareContent;

public class SharePopupWindow extends PopupWindow implements OnClickListener {
	
	//微信ID
	String wxAppID = "wx680756aafc0a2e97";
	
	private static final String TAG = "SharePopupWindow";

	private static final String SHARE_TITLE = "新道app测试";
	private RelativeLayout rlShareToSina;
	private RelativeLayout rlShareToWeixin;
	private RelativeLayout rlShareToFriends;
	private RelativeLayout rlShareToSms;
	private UMSocialService mController;
	private String shareContent;
	private String urlContent;
	private Context mContext;

	public SharePopupWindow(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Logger.i(TAG, "attrs");
//		init(context);
	}

	public void setInfors(UMSocialService mController, String shareContent,
			String urlContent) {
		this.mController = mController;
		this.shareContent = shareContent;
		this.urlContent = urlContent;
	}

	public void sendReq(Context context, String text, Bitmap bmp, int type) {
		IWXAPI api = WXAPIFactory.createWXAPI(context, wxAppID, true);
		if (!api.isWXAppInstalled()) {
			// UIHelper.showToast(context, "您没有安装微信，请下载后分享...");
			return;
		}

		String url = urlContent;// 收到分享的好友点击信息会跳转到这个地址去
		WXWebpageObject localWXWebpageObject = new WXWebpageObject();
		localWXWebpageObject.webpageUrl = url;
		WXMediaMessage localWXMediaMessage = new WXMediaMessage(
				localWXWebpageObject);
		localWXMediaMessage.title = SHARE_TITLE;// 不能太长，否则微信会提示出错。不过博主没验证过具体能输入多长。
		localWXMediaMessage.description = text;
		localWXMediaMessage.thumbData = getBitmapBytes(bmp, false);
		SendMessageToWX.Req localReq = new SendMessageToWX.Req();
		localReq.transaction = System.currentTimeMillis() + "";
		localReq.message = localWXMediaMessage;
		localReq.scene = type;
		api.sendReq(localReq);
	}

	// 需要对图片进行处理，否则微信会在log中输出thumbData检查错误
	private static byte[] getBitmapBytes(Bitmap bitmap, boolean paramBoolean) {
		Bitmap localBitmap = Bitmap.createBitmap(80, 80, Bitmap.Config.RGB_565);
		Canvas localCanvas = new Canvas(localBitmap);
		int i;
		int j;
		if (bitmap.getHeight() > bitmap.getWidth()) {
			i = bitmap.getWidth();
			j = bitmap.getWidth();
		} else {
			i = bitmap.getHeight();
			j = bitmap.getHeight();
		}
		while (true) {
			localCanvas.drawBitmap(bitmap, new Rect(0, 0, i, j), new Rect(0, 0,
					80, 80), null);
			if (paramBoolean)
				bitmap.recycle();
			ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
			localBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
					localByteArrayOutputStream);
			localBitmap.recycle();
			byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
			try {
				localByteArrayOutputStream.close();
				return arrayOfByte;
			} catch (Exception e) {

			}
			i = bitmap.getHeight();
			j = bitmap.getHeight();
		}
	}

	private void init(Context context) {
		mContext = context;
		View convertView = View.inflate(context, R.layout.popup, null);
		rlShareToFriends = (RelativeLayout) convertView
				.findViewById(R.id.rl_share_to_friends);
		rlShareToWeixin = (RelativeLayout) convertView
				.findViewById(R.id.rl_share_to_weixin);
		rlShareToSina = (RelativeLayout) convertView
				.findViewById(R.id.rl_share_to_sina);
		rlShareToSms = (RelativeLayout) convertView
				.findViewById(R.id.rl_share_to_sms);
		rlShareToFriends.setOnClickListener(this);
		rlShareToWeixin.setOnClickListener(this);
		rlShareToSina.setOnClickListener(this);
		rlShareToSms.setOnClickListener(this);
		setBackgroundDrawable(new PaintDrawable(Color.TRANSPARENT));
		setOutsideTouchable(true);
	}

	public SharePopupWindow(Context context, AttributeSet attrs) {
		super(context, attrs);
		Logger.i(TAG, "attrs");
//		init(context);

	}

	public SharePopupWindow(Context context) {
		super(context);
		Logger.i(TAG, "context");
//		init(context);
	}

	public SharePopupWindow(Context context, View convertView, int width,
			int height) {
		super(convertView, width, height);
		mContext = context;
		init(context, convertView);
		Logger.i(TAG, "convertView");
	}

	private void init(Context context, View convertView) {
//		mContext = context;
		Logger.i(TAG, "show init");
		rlShareToFriends = (RelativeLayout) convertView
				.findViewById(R.id.rl_share_to_friends);
		rlShareToWeixin = (RelativeLayout) convertView
				.findViewById(R.id.rl_share_to_weixin);
		rlShareToSina = (RelativeLayout) convertView
				.findViewById(R.id.rl_share_to_sina);
		rlShareToSms = (RelativeLayout) convertView
				.findViewById(R.id.rl_share_to_sms);
		rlShareToFriends.setOnClickListener(this);
		rlShareToWeixin.setOnClickListener(this);
		rlShareToSina.setOnClickListener(this);
		rlShareToSms.setOnClickListener(this);
		setBackgroundDrawable(new PaintDrawable(Color.TRANSPARENT));
		setOutsideTouchable(true);
	}

	@Override
	public void onClick(View view) {
		
		Logger.i(TAG, "点击");
		
		switch (view.getId()) {
		case R.id.rl_share_to_friends:// 朋友圈
			if (isShowing()) {
				dismiss();
			}

			Logger.i(TAG, "weixin朋友圈");
			// 支持微信朋友圈
//			UMWXHandler handler;
//			mController.getConfig().set
			mController.getConfig().supportWXCirclePlatform(mContext, wxAppID,
					urlContent);
			mController.setShareImage(new UMImage(mContext, R.drawable.share_icon));
			mController.setShareContent(shareContent + urlContent);
			
			mController.postShare(mContext, SHARE_MEDIA.WEIXIN_CIRCLE,
					new SnsPostListener() {
						@Override
						public void onComplete(SHARE_MEDIA arg0, int arg1,
								SocializeEntity arg2) {
							PromptManager.showToast(mContext, "完成分享");
							
						}

						@Override
						public void onStart() {
							PromptManager.showToast(mContext, "开始分享");
						}
					});
//			sendReq(mContext,
//					shareContent+urlContent,
////					content,
////					BitmapFactory.decodeFile(imagePath),
//					BitmapFactory.decodeResource(
//							mContext.getResources(),
//							R.drawable.icon_share),
//					Req.WXSceneSession);

			break;
		case R.id.rl_share_to_sina:// 新浪微博
			
			Logger.i(TAG, "sina点击");
			if (isShowing()) {
				dismiss();
			}
//			mController.
			mController.setShareContent(shareContent + urlContent);
			mController.setShareImage(new UMImage(mContext, R.drawable.share_icon));
			
			mController.initEntity(mContext, new SocializeClientListener() {
				
				@Override
				public void onStart() {
				}
				
				@Override
				public void onComplete(int arg0, SocializeEntity arg1) {
				}
			});
			
			mController.postShare(mContext, SHARE_MEDIA.SINA,
					new SnsPostListener() {
						@Override
						public void onComplete(SHARE_MEDIA arg0, int arg1,
								SocializeEntity arg2) {
						}

						@Override
						public void onStart() {
						}
					});
			break;
		case R.id.rl_share_to_weixin:// 微信
			if (isShowing()) {
				dismiss();
			}
			// 支持微信好友分享
			mController.getConfig().supportWXPlatform(mContext, wxAppID,
					urlContent);
			// 构建 UMImage,参数 1 为当前的 activity  参数 2 为图片的链接
			UMImage shareImage = new UMImage(mContext,
			"http://www.umeng.com/images/pic/banner_module_social.png");
			//  设置微信平台的分享内容
			// 微信平台的分享内容, 多媒体内容设置为图片, 且只能设置为一种类型
			WeiXinShareContent weixinContent = new WeiXinShareContent(shareImage);
			weixinContent.setShareContent("这是新道app主要内容");
			weixinContent.setTitle("新道app标题");
			
			mController.setShareMedia(weixinContent);
			
//			mController.setShareImage(new UMImage(mContext, R.drawable.share_icon));
//			mController.setShareContent(shareContent + urlContent);
//			mController.setEntityName("entity");
//			
//			mController.setConfig(arg0 );
			mController.postShare(mContext, SHARE_MEDIA.WEIXIN,
					new SnsPostListener() {
						@Override
						public void onComplete(SHARE_MEDIA arg0, int arg1,
								SocializeEntity arg2) {
						}

						@Override
						public void onStart() {

						}
					});
			break;
		case R.id.rl_share_to_sms:// 短信
			if (isShowing()) {
				dismiss();
			}
			
			Intent intent = new Intent(Intent.ACTION_SENDTO);
		    intent.putExtra("sms_body", shareContent);
//		    intent.setData(Uri.parse("smsto:" ));
//		    return intent;
//			Intent smsintent = IntentUtils.sendSMS("", shareContent
//					+ urlContent);
//			mContext.startActivity(smsintent);
			mContext.startActivity(intent);
			break;
		default:
			break;
		}
	}

}
